package com.todolist.service.impl;

import com.todolist.dao.TTodolist;
import com.todolist.dao.User;
import com.todolist.mapper.UserMapper;
import com.todolist.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountNotFoundException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 用户登录
     *
     * @param
     * @return
     */
    public User login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();

        User user1 = userMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user1 == null) {
            //账号不存在
            try {
                throw new AccountNotFoundException("没有找到");
            } catch (AccountNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        //密码比对
        if (!password.equals(user1.getPassword())) {
            //密码错误
            try {
                throw new Exception("密码错误");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        //返回结果
        return user1;
    }


    /**
     * 用户注册
     *
     * @param
     * @return
     */
    public void  regitser(User user) {
        //去数据库中查询
        User user1 = userMapper.getByUsername(user.getUsername());
        //如果没有就去注册
        if(user1 == null){
          userMapper.register(user);
        }else{
            System.out.println("当前对象已存在");
        }

    }


}
